package com.youlu.campus.web.pay;

import com.youlu.campus.entity.UserPO;
import com.youlu.campus.entity.VO.req.ShoppingCartComputeReq;
import com.youlu.campus.service.pay.UserPurchaseCaService;
import com.youlu.campus.web.common.base.RequestContext;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * 用户否买证书V2接口
 */
@Slf4j
@RestController
public class UserPurchaseCaV2Controller {
    @Autowired
    private UserPurchaseCaService userPurchaseCaService;

    @GetMapping("/mini-app/v2/item/category")
    public QResult getItemCategory(@RequestParam("type") String type,
                                   @RequestParam("activityId") String activityId) {
        return QResult.success(userPurchaseCaService.getItemCategory(type, activityId));
    }

    @GetMapping("/mini-app/v2/item/index")
    public QResult IndexInfo(@RequestParam("activityId") String activityId,
                             @RequestParam(value = "mobile",required = false) String mobile) {
        UserPO userPO=RequestContext.getCurUser();
        if(StringUtils.isNotBlank(mobile)){
            userPO.setMobile(mobile);
        }
        return QResult.success(userPurchaseCaService.getIndexInfo(userPO, activityId));
    }

    @GetMapping("/mini-app/v2/mall/front-category")
    public QResult getFrontCategory(@RequestParam("activityId") String activityId,
                                    @RequestParam(value = "mobile",required = false) String mobile) {
        UserPO userPO=RequestContext.getCurUser();
        if(StringUtils.isNotBlank(mobile)){
            userPO.setMobile(mobile);
        }
        return QResult.success(userPurchaseCaService.getFrontCategories(activityId,userPO));
    }

    @GetMapping("/mini-app/v2/category/items")
    public QResult getItemsByCategoryId(@RequestParam("categoryId") String categoryId,
                                        @RequestParam("activityId") String activityId,
                                        @RequestParam(value = "mobile",required = false) String mobile) {
        log.info(":>>> 获取分类的商品信息:{},{},{}",activityId,categoryId,mobile);
        UserPO userPO=RequestContext.getCurUser();
        if(StringUtils.isNotBlank(mobile)){
            log.info(":>>> 获取分类的商品,传入手机号:{},用户本身手机号:{}",mobile,userPO.getMobile());
            userPO.setMobile(mobile);
        }
        return QResult.success(userPurchaseCaService.getItems(activityId, categoryId,userPO));
    }

    /**
     * 购物车计算价格
     *
     * @param r
     * @return
     */
    @PostMapping("/mini-app/v2/compute/cart-fee")
    public QResult computeCartFee(@RequestBody @NotNull ShoppingCartComputeReq r) {
        r.setUserId(RequestContext.getUserId());
        return QResult.success(userPurchaseCaService.computeCartFee(r));
    }

    /**
     * 获取购物车商品
     *
     * @param aid
     * @return
     */
    @PostMapping("/mini-app/v2/cart/items")
    public QResult getCartItems(@RequestParam("activityId") String aid,
                                @RequestParam(value = "mobile",required = false) String mobile) {
        UserPO userPO=RequestContext.getCurUser();
        if(StringUtils.isNotBlank(mobile)){
            userPO.setMobile(mobile);
        }
        return QResult.success(userPurchaseCaService.getShoppingCartItems(aid,userPO));
    }
}
